﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// Window3.xaml 的交互逻辑
    /// </summary>
    public partial class Window3 : Window
    {
        public Window3()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Do1();
        }
        void Do1()
        {
            while (true)
            {
                try
                {
                    Thread.Sleep(1000);

                    Socket   socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                    socket.Connect("192.168.1.98", 10001);
                    var r = socket.Poll(100, SelectMode.SelectRead);
                    var wr = socket.Poll(100, SelectMode.SelectWrite);
                    Console.WriteLine("{0},{1}", r, wr);
                    if (wr)
                    {
                        var send = Encoding.Default.GetBytes("@00TS000146*\r");
                        var wlen = socket.Send(send);
                        var buff = new byte[1024];
                        var rlen = socket.Receive(buff);
                        Console.WriteLine("rlen:{0}", rlen);
                        Console.WriteLine("wlen:{0}", wlen);

                    }
                    if (r)
                    {
                        var buff = new byte[1024];
                        var rlen = socket.Receive(buff);
                        Console.WriteLine("rlen:{0}", rlen);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }

            }
            
        }
    }
}
